package com.dom.domweb.modular.controller.sysmger;

import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.common.TabPager;
import com.dom.domweb.dao.service.ITUserInfoService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.util.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Author xllei
 * @Date 2018/11/22 11:24 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/sys")
public class UserController {
    @Autowired
    ITUserInfoService userInfoService;

    @RequestMapping("/getUserList")
    public TabPager getUserList(@RequestParam("limit")int limit, @RequestParam("page")int page, @RequestParam("userName") String userName,
                                @RequestParam("organCode")String organCode, @RequestParam("stat")String stat){
        ShiroUser user = ShiroUtil.getUser();

        B079R request = new B079R();
        request.setPn(page);
        request.setPs(limit == 0 ? 20 : limit);
        request.setUserName(userName);
        if(StringUtils.isNotEmpty(organCode)){
            request.setOrganCode(Integer.valueOf(organCode));
        }
        if(StringUtils.isNotEmpty(stat)){
            request.setStat(Integer.valueOf(stat));
        }

        B079A result = userInfoService.findUserByPager(request, user);

        return new TabPager(result.getUserInfoData(), result.getTotalNum());
    }

    @RequestMapping("/addUserInfo")
    public RspModel addUserInfo(@RequestParam("userName")String userName, @RequestParam("organCode")Integer organCode,
                                @RequestParam("phone")String phone, @RequestParam("roleCode")Integer roleCode){
        ShiroUser user = ShiroUtil.getUser();

        B070R request = new B070R();
        request.setUserName(userName);
        request.setOrganCode(organCode);
        request.setPhone(phone);
        request.setRoleId(roleCode);

        B070A response = userInfoService.addUserInfo(request, user);
        return new RspModel(response.getUserId());
    }

    @RequestMapping("/editUserInfo")
    public RspModel editUserInfo(@RequestParam("id")Integer id, @RequestParam("userName")String userName,
                                 @RequestParam("organCode")Integer organCode, @RequestParam("phone")String phone,
                                 @RequestParam("roleCode")Integer roleCode, @RequestParam("stat")Integer stat){
        B072R request = new B072R();
        if(0 == id){
            return new RspModel("RSP090001", "请选择用户修改");
        }
        request.setUserId(id);
        request.setUserName(userName);
        request.setOrganCode(organCode);
        request.setPhone(phone);
        request.setRoleCode(roleCode);
        request.setStat(stat);

        B072A response = userInfoService.editUserInfo(request);
        return new RspModel(response.getUserId());
    }

    @RequestMapping("/delUserInfo")
    public RspModel delUserInfo(@RequestParam("id") Integer id){
        if(0 == id){
            return new RspModel("RSP090001", "请选择用户修改");
        }

        B071R request = new B071R();
        request.setUserId(id);

        B071A response = userInfoService.delUserInfo(request);
        return new RspModel(response.getUserId());
    }
}
